package ConveyorBelt.State;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;

import ConveyorBelt.Util.FontUtil;
import ConveyorBelt.Util.ImageUtil;

public class Pause extends BasicGameState {
	public static int stateID = 4;
	private int	resumeGameButtonX, resumeGameButtonY;
	private int exitGameButtonX, exitGameButtonY;
	private int mouseX, mouseY;
    private FadeOutTransition fot;
    private FadeInTransition fit;
	private float resumeGameScale = 1;
	private float exitGameScale = 1;
	private float scaleStep = 0.0001f;
	
	@Override
	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
		ImageUtil.loadAllImage();
		FontUtil.loadGameFont();
		resumeGameButtonX = gc.getWidth()/2 - ImageUtil.resumeGameButton.getWidth()/2;
		resumeGameButtonY = gc.getHeight()/2 - 100;
		exitGameButtonX = resumeGameButtonX;
		exitGameButtonY = resumeGameButtonY + 100;
	}

	@Override
	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
		ImageUtil.background.draw(0, 0);
		ImageUtil.resumeGameButton.draw(resumeGameButtonX, resumeGameButtonY, resumeGameScale);
		ImageUtil.exitGameButton.draw(exitGameButtonX, exitGameButtonY, exitGameScale);
		g.setFont(FontUtil.uf);
 		g.drawString("OOP 2D Game Project:      Can You Help Me???      - Author: Phu - Manh - Quang", 225, 650);
	}

	@Override
	public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
		Input input = gc.getInput();
		mouseX = input.getMouseX();
		mouseY = input.getMouseY();
		boolean insideResumeGame = false; 
		boolean insideExitGame = false;
		int tmpX;
		int tmpY;
		fot = new FadeOutTransition(Color.black);
    	fit = new FadeInTransition(Color.black);
    	
    	if((mouseX >= exitGameButtonX && mouseX <= exitGameButtonX + ImageUtil.exitGameButton.getWidth()) && 
				(mouseY >= exitGameButtonY && mouseY <= exitGameButtonY + ImageUtil.exitGameButton.getHeight())) {
			insideExitGame = true;
		}
		tmpY = gc.getHeight()/2;
		if(insideExitGame) {
			tmpX = gc.getWidth()/2 - ImageUtil.exitGameButton.getWidth()/2 + 10;
			if(exitGameScale < 1.05f) exitGameScale += scaleStep*delta;
			if(exitGameButtonX < tmpX) exitGameButtonX ++;
			if(exitGameButtonY >= tmpY) exitGameButtonY --;
			if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) gc.exit();
		}
		else {
			tmpX = gc.getWidth()/2 - ImageUtil.exitGameButton.getWidth()/2;
			if(exitGameScale > 1.0f) exitGameScale -= scaleStep*delta;
			if(exitGameButtonX > tmpX) exitGameButtonX--;
			if(exitGameButtonY <= tmpY) exitGameButtonY++;
		}
		
		if((mouseX >= resumeGameButtonX && mouseX <= resumeGameButtonX + ImageUtil.resumeGameButton.getWidth()) && 
				(mouseY >= resumeGameButtonY && mouseY <= resumeGameButtonY + ImageUtil.resumeGameButton.getHeight())) {
			insideResumeGame = true;
		}
		tmpY = gc.getHeight()/2 - 100;
		if(insideResumeGame) {
			tmpX = gc.getWidth()/2 - ImageUtil.resumeGameButton.getWidth()/2 + 10;
			if(resumeGameScale < 1.05f) resumeGameScale += scaleStep*delta;
			if(resumeGameButtonX < tmpX) resumeGameButtonX ++;
			if(resumeGameButtonY >= tmpY) resumeGameButtonY --;
			if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) sbg.enterState(Game.stateID, fot, fit);
		}
		else {
			tmpX = gc.getWidth()/2 - ImageUtil.resumeGameButton.getWidth()/2;
			if(resumeGameScale > 1.0f) resumeGameScale -= scaleStep*delta;
			if(resumeGameButtonX > tmpX) resumeGameButtonX--;
			if(resumeGameButtonY <= tmpY) resumeGameButtonY++;
		}
	}

	@Override
	public int getID() {
		return stateID;
	}
}
